#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ql_oe.h"
#include "ql_spp.h"
#include "ql_ble_uuid.h"
#include "ql_ble_types.h"


typedef int (*TEST)(void);

typedef struct
{
    int  cmdIdx;
    char *funcName;
} spp_api_test_case;

typedef struct
{
    char                 *group_name;
    spp_api_test_case    *test_cases;
    TEST                  pf_test;
} func_api_spp_test_t;

void show_group_spp_help(func_api_spp_test_t *pt_test);
int test_bt_func(void);

#define GROUP_MAX 10

func_api_spp_test_t spp_test_list[GROUP_MAX] = {0};

void add_spp_test_node(func_api_spp_test_t *pt_test)
{
    int i;
    for(i=0; i<GROUP_MAX; i++)
    {
        if(spp_test_list[i].group_name == NULL)
        {
            spp_test_list[i].group_name   = pt_test->group_name;
            spp_test_list[i].test_cases   = pt_test->test_cases;
            spp_test_list[i].pf_test      = pt_test->pf_test;
            break;
        }
    }
}


void show_group_spp_help(func_api_spp_test_t *pt_test)
{
    int i;

    printf("Group Name:%s, Supported test cases:\n", pt_test->group_name);
    for(i = 0; ; i++)
    {
        if(pt_test->test_cases[i].cmdIdx == -1)
        {
            break;
        }
        printf("%d:\t%s\n", pt_test->test_cases[i].cmdIdx, pt_test->test_cases[i].funcName);
    }
}



void show_test_list()
{
    int i;

    printf("Test groups:\n");
    for(i = 0; i<GROUP_MAX; i++)
    {
        if(spp_test_list[i].group_name != NULL)
        {
            printf("%d:\t%s\n", i, spp_test_list[i].group_name);
        }
    }
}


spp_api_test_case bt_spp_testlist[] = 
{
    {0,     "power on"},
    {1,     "power off"},
    {2,     "client init"},
    {3,     "request instances"},
    {4,     "register spp"},
    {5,     "activate spp"},
    {6,     "disconnect spp"},
    {7,     "deactivate spp"},
    {8,     "write spp"},
	{9,     "get local address"},
    {-1,    NULL}
};

func_api_spp_test_t spp_bt_test = {"spp_test", bt_spp_testlist, test_bt_func};

const char leRole_str[][32] = {"undefined","master","slave"};

void demo_trace_dump_hex(char *prompt, unsigned char *buf, int len)
{
    int i;

    if (len > 0)
    {
        printf("%s:\n", prompt);
        for (i = 0; i < len; i++)
        {
            printf("%02X ", buf[i]);
            if ((i + 1) % 16 == 0)
            {
                printf("\n");
            }
        }
        if (i % 16 != 0)
        {
            printf("\n");
        }
    }
}

//<2020/03/05>add colin Callback function interface
//typedef void (*reback_func)(char *buf);
void printf_cb(char *buf)
{
    int i;
    for (i = 0; i < 1024; i++)
    {
       printf("%c",buf[i]);
    }
    printf("\n");
}


int test_bt_func(void)
{  
    int     cmdIdx  = 0;
    int     ret;

    show_group_spp_help(&spp_bt_test);

    while(1)
    {
        printf("please input cmd index(-1 exit): ");
        scanf("%d", &cmdIdx);
        if(cmdIdx == -1)
        {
            break;
        }
        switch(cmdIdx)
        {
            case 0:
            {
                ret = ql_spp_power_on();
                printf("ql_spp_power_on, ret=%d\n", ret);
            }
            break;
            case 1:
            {
                ret = ql_spp_power_off();
                printf("ql_spp_power_off, ret=%d\n", ret);
            }
            break;
            case 2:
            {//<2020/03/05>modify colin Callback function interface
                ret = ql_spp_client_init(printf_cb);
                printf("ql_spp_client_init, ret=%d\n", ret);
            }
            break;
            case 3:
            {
                ret = ql_instance_spp();
                printf("ret=%d\n", ret);
            }
            break;
            case 4:
            {
                ret = ql_register_spp();
                printf("ret=%d\n", ret);
            }
            break;
            case 5:
            {
                ret = ql_activate_spp();
                printf("ret=%d\n", ret);
            }
            break;
            case 6:
            {
                ret = ql_disconnect_spp();
                printf("ret=%d\n", ret);
                
            }
            break;
            case 7:
            {
                ret = ql_deactivate_spp();
                printf("ret=%d\n", ret);
                
            }
            break;
            case 8:
            {
				unsigned char msg[256] = {0};
				printf("Please input the msg buff:\n");
		        scanf("%s", msg);
                ret = ql_spp_tx_msg((QuecBtUint16)strlen(msg), msg);
                printf("ret=%d\n", ret);
                
            }
            break;
			case 9:
            {
                char mac[18] = {0};
				ret = ql_bt_mac_get(mac);
				if (ret == 0){
					printf("get bt mac success. mac = %s. \n", mac);
				}else{
					printf("get bt mac failure. mac = %s. \n", mac);
				}
            }break;
            default:
            break;
        }
    }
    return 0;
}



int main(int argc, const char** argv)
{

    int cmdIdx = 0;
    add_spp_test_node(&spp_bt_test);
    show_test_list();
    while(1)
    {
        printf("please input command index(-1 exit): ");
        scanf("%d", &cmdIdx);
        if(cmdIdx == -1)
        {
            break;
        }
        if((cmdIdx >= GROUP_MAX) || (spp_test_list[cmdIdx].group_name == NULL))
        {
            show_test_list();
        }
        else
        {
            spp_test_list[cmdIdx].pf_test();
        }
    }
    return 0;
}

